import { Menu, MenuItemConstructorOptions, app, shell } from 'electron';

export class MenuManager {
  private menu: Menu | null = null;

  public setupMenu(): void {
    const template: MenuItemConstructorOptions[] = [
      {
        label: '文件',
        submenu: [
          {
            label: '新建连接',
            accelerator: 'CmdOrCtrl+N',
            click: () => {
              // TODO: 实现新建连接功能
            },
          },
          {
            label: '快速连接',
            accelerator: 'CmdOrCtrl+K',
            click: () => {
              // TODO: 实现快速连接功能
            },
          },
          { type: 'separator' },
          {
            label: '导入配置',
            click: () => {
              // TODO: 实现导入配置功能
            },
          },
          {
            label: '导出配置',
            click: () => {
              // TODO: 实现导出配置功能
            },
          },
          { type: 'separator' },
          {
            label: '退出',
            accelerator: process.platform === 'darwin' ? 'Cmd+Q' : 'Ctrl+Q',
            click: () => {
              app.quit();
            },
          },
        ],
      },
      {
        label: '编辑',
        submenu: [
          {
            label: '撤销',
            accelerator: 'CmdOrCtrl+Z',
            role: 'undo',
          },
          {
            label: '重做',
            accelerator: 'Shift+CmdOrCtrl+Z',
            role: 'redo',
          },
          { type: 'separator' },
          {
            label: '剪切',
            accelerator: 'CmdOrCtrl+X',
            role: 'cut',
          },
          {
            label: '复制',
            accelerator: 'CmdOrCtrl+C',
            role: 'copy',
          },
          {
            label: '粘贴',
            accelerator: 'CmdOrCtrl+V',
            role: 'paste',
          },
          {
            label: '全选',
            accelerator: 'CmdOrCtrl+A',
            role: 'selectAll',
          },
        ],
      },
      {
        label: '视图',
        submenu: [
          {
            label: '切换侧边栏',
            accelerator: 'CmdOrCtrl+B',
            click: () => {
              // TODO: 实现切换侧边栏功能
            },
          },
          { type: 'separator' },
          {
            label: '连接管理',
            accelerator: 'CmdOrCtrl+1',
            click: () => {
              // TODO: 实现切换到连接管理页面
            },
          },
          {
            label: '终端会话',
            accelerator: 'CmdOrCtrl+2',
            click: () => {
              // TODO: 实现切换到终端会话页面
            },
          },
          {
            label: '文件管理',
            accelerator: 'CmdOrCtrl+3',
            click: () => {
              // TODO: 实现切换到文件管理页面
            },
          },
          {
            label: 'AI助手',
            accelerator: 'CmdOrCtrl+4',
            click: () => {
              // TODO: 实现切换到AI助手页面
            },
          },
          {
            label: '系统设置',
            accelerator: 'CmdOrCtrl+5',
            click: () => {
              // TODO: 实现切换到系统设置页面
            },
          },
          { type: 'separator' },
          {
            label: '重新加载',
            accelerator: 'CmdOrCtrl+R',
            click: () => {
              // TODO: 实现重新加载功能
            },
          },
          {
            label: '强制重新加载',
            accelerator: 'CmdOrCtrl+Shift+R',
            click: () => {
              // TODO: 实现强制重新加载功能
            },
          },
          {
            label: '开发者工具',
            accelerator: process.platform === 'darwin' ? 'Alt+Cmd+I' : 'Ctrl+Shift+I',
            click: () => {
              // TODO: 实现开发者工具切换功能
            },
          },
          { type: 'separator' },
          {
            label: '实际大小',
            accelerator: 'CmdOrCtrl+0',
            role: 'resetZoom',
          },
          {
            label: '放大',
            accelerator: 'CmdOrCtrl+Plus',
            role: 'zoomIn',
          },
          {
            label: '缩小',
            accelerator: 'CmdOrCtrl+-',
            role: 'zoomOut',
          },
          { type: 'separator' },
          {
            label: '全屏',
            accelerator: 'F11',
            role: 'togglefullscreen',
          },
        ],
      },
      {
        label: '窗口',
        submenu: [
          {
            label: '最小化',
            accelerator: 'CmdOrCtrl+M',
            role: 'minimize',
          },
          {
            label: '关闭',
            accelerator: 'CmdOrCtrl+W',
            role: 'close',
          },
        ],
      },
      {
        label: '帮助',
        submenu: [
          {
            label: '关于',
            click: () => {
              // TODO: 实现关于对话框
            },
          },
          {
            label: '帮助文档',
            click: () => {
              shell.openExternal('https://github.com/your-username/modern-ssh-client');
            },
          },
          {
            label: '报告问题',
            click: () => {
              shell.openExternal('https://github.com/your-username/modern-ssh-client/issues');
            },
          },
          {
            label: '检查更新',
            click: () => {
              // TODO: 实现检查更新功能
            },
          },
        ],
      },
    ];

    this.menu = Menu.buildFromTemplate(template);
    Menu.setApplicationMenu(this.menu);
  }

  public getMenu(): Menu | null {
    return this.menu;
  }

  public setMenu(menu: Menu | null): void {
    this.menu = menu;
    Menu.setApplicationMenu(menu);
  }
}